package com.pxst.fi.service.impl;

import com.pxst.fi.entity.FiExcelModel;
import com.pxst.fi.entity.ShTrxExcelModel;
import com.pxst.fi.entity.SysAdjustOrderEntity;
import com.pxst.fi.entity.SysFiOrderEntity;
import com.pxst.fi.req.MerchantStatementReq;
import com.pxst.fi.resp.FiOrderResp;
import com.pxst.fi.resp.MerchantStatementResp;
import com.pxst.fi.service.FiService;
import com.pxst.fi.service.MerchantStatementService;
import com.pxst.fi.service.SysAdjustOrderService;
import com.pxst.fo.sys.entity.FoOrderMainEntity;
import com.pxst.fo.sys.service.FoOrderMainService;
import com.pxst.fx.entity.SysFxOrderEntity;
import com.pxst.fx.service.SysFxOrderEntityService;
import com.pxst.merchant.entity.SysMerchantInfo;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.AltIdUtils;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.pxst.fi.constants.AdjustOrderConstant.*;
import static com.pxst.fi.constants.FiConstant.*;

/**
 * @author rj
 * @create 2023-09-19-16:07
 */
@Service
public class MerchantStatementServiceImpl implements MerchantStatementService {

    @Autowired
    private SysAdjustOrderService adjustOrderService;

    @Autowired
    private FoOrderMainService foOrderMainService;

    @Autowired
    private SysFxOrderEntityService fxOrderEntityService;

    @Autowired
    private FiService fiService;

    @Override
    public ApiResp<PageResp<MerchantStatementResp>> getAllByCondition(MerchantStatementReq req) {
        // 查询数据
        List<MerchantStatementResp> respList = getMerchantStatementResps(req);
        // 总条数
        Integer total = respList.size();
        // 计算总计
        Map<String, BigDecimal> totals = getSubTotalsOrTotals(respList);
        // 逻辑分页
        respList = respList.stream().skip((req.getPage() - 1) * req.getRp()).limit(req.getRp()).collect(Collectors.toList());
        // 计算小计
        Map<String, BigDecimal> subTotals = getSubTotalsOrTotals(respList);

          return ApiResp.page(req.getPage(), req.getRp(), total, respList, subTotals, totals);
    }

    @Override
    public List<ShTrxExcelModel> download(MerchantStatementReq req) {
        // 查询数据
        List<MerchantStatementResp> respList = getMerchantStatementResps(req);
        // 为模型类添加数据
        List<ShTrxExcelModel> excelModels = addDataForExcel(respList);
        return excelModels;
    }

    /**
     * 为excel表格添加数据
     *
     * @param respList
     * @return
     */
    private List<ShTrxExcelModel> addDataForExcel(List<MerchantStatementResp> respList) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return respList.stream().map(resp -> {
            ShTrxExcelModel shTrxExcelModel = new ShTrxExcelModel();
            shTrxExcelModel.setId(resp.getAltId());
            shTrxExcelModel.setMerchant(StringConcatNotNull(resp.getMerchantCode(), "-", resp.getMerchantName()));
            shTrxExcelModel.setType(getType(resp));
            shTrxExcelModel.setReason(processReason(resp));
            shTrxExcelModel.setRequestedAmount(resp.getRequestAmount());
            shTrxExcelModel.setDebit(resp.getDebit());
            shTrxExcelModel.setCredit(resp.getCredit());
            shTrxExcelModel.setFee(resp.getRate());
            shTrxExcelModel.setCurrentBalance(resp.getBalance());
            shTrxExcelModel.setBankFee(resp.getBankFee());
            shTrxExcelModel.setBankFeeBearer(ObjectUtils.isEmpty(resp.getPayBankFee()) ? null : processPayBankFee(resp));
            shTrxExcelModel.setDescription(resp.getDescription());
            shTrxExcelModel.setTxnDate(format.format(resp.getTransactionDate()));
            return  shTrxExcelModel;
        }).collect(Collectors.toList());
    }

    /**
     * 处理汇出报表payBankFee值
     *
     * @param resp
     * @return
     */
    private String processPayBankFee(MerchantStatementResp resp) {
        Map<Integer, String> payBankFeeMap = new HashMap<>();
        payBankFeeMap.put(0, "one wallet/壹支付");
        payBankFeeMap.put(1, "merchant/商户");
        return payBankFeeMap.get(resp.getPayBankFee());
    }

    /**
     * 处理汇出报表的reason值
     * @param resp
     * @return
     */
    private String processReason(MerchantStatementResp resp) {
        Map<Integer, String> reasonMap = new HashMap<>();
        reasonMap.put(REASON_HUMAN, "Human error/人为错误");
        reasonMap.put(REASON_SYSTEM, "System error/系统错误");
        reasonMap.put(REASON_FREEZE, "Freeze/冻结");
        reasonMap.put(REASON_UNFREEZE, "Unfreeze/解除冻结");
        reasonMap.put(REASON_FEE, "Fee/手续费");
        reasonMap.put(REASON_OTHERS, "Others/其他");
        reasonMap.put(REASON_REBATE, "Rebate/返点");
        reasonMap.put(REASON_REFUND, "Refund/退款");
        reasonMap.put(REASON_DOUBLE_PAYOUT, "Double payout/重复出款");
        reasonMap.put(REASON_BANK_REVERSAL, "Bank Reversal/银行冲帐");
        reasonMap.put(REASON_TOPUP, "Topup/内充");
        return reasonMap.get(resp.getReason());
    }

    /**
     * 获取汇出报表的type值
     * @param resp
     * @return
     */
    private String getType(MerchantStatementResp resp) {
        Map<Integer, String> typeMap = new HashMap<>();
        typeMap.put(TXN_MODE_BANK, "Bank Deposit/银行充值");
        typeMap.put(TXN_MODE_QRPAY, "QR Deposit/QR充值");
        typeMap.put(TXN_MODE_TRUE_WALLET, "TrueWallet Deposit/TrueWallet充值");
        typeMap.put(TXN_MODE_VNPAY_ZALO_QR, "Zalo QR/Zalo 扫码");
        typeMap.put(TXN_MODE_VNPAY_MOMO_QR, "Momo QR/Momo 扫码");
        typeMap.put(TXN_MODE_VNPAY_VIETTEL_QR, "Viettelpay QR/Viettelpay 扫码");
        typeMap.put(TXN_MODE_VNPAY_BANK_QR, "Bank QR/银行卡扫码");
        typeMap.put(TXN_MODE_VNPAY_BANK, "Onling banking/网银");
        typeMap.put(TXN_MODE_VNPAY_BANK_CARD, "Bank transfer/银行卡转账");

        Integer type = resp.getType();
        switch (type) {
            case 0:
                return typeMap.get(resp.getTxnMode());
            case 1:
                return "Withdraw/代付";
            case 2:
                return "Settlement/下发";
            case 3:
                return "Topup/内充";
            case 4:
                return "Balance/余额";
            case 5:
                return "Settlement/下发";
        }
        return null;
    }

    /**
     * 字符串拼接不拼接null的方法
     * @return
     */
    private String StringConcatNotNull(String... args){
        StringBuilder builder = new StringBuilder();
        for (String arg : args) {
            if (arg != null){
                builder.append(arg);
            }
        }
        return builder.toString().trim();
    }

    /**
     * 获取查询数据
     * @param req
     * @return
     */
    private List<MerchantStatementResp> getMerchantStatementResps(MerchantStatementReq req) {
        List<Integer> type = req.getType();
        List<MerchantStatementResp> respList = new ArrayList<>();
        if (type == null || type.size() == 0){
            respList.addAll(getAjSource(req));
            respList.addAll(getFoSource(req));
            respList.addAll(getFxSource(req));
            respList.addAll(getFiSource(req, null));
        }else {
            for (Integer integer : type) {
                switch (integer) {
                    case 0:
                        respList.addAll(getAjSource(req));
                        break;
                    case 1:
                        respList.addAll(getFoSource(req));
                        respList.addAll(getAjSource(req));
                        break;
                    case 2:
                        respList.addAll(getFxSource(req));
                        respList.addAll(getAjSource(req));
                        break;
                    case 3:
                    case 4:
                        respList.addAll(getAjSource(req));
                        break;
                    case 5:
                        respList.addAll(getFiSource(req, TXN_MODE_BANK));
                        break;
                    case 6:
                        respList.addAll(getFiSource(req, TXN_MODE_QRPAY));
                        break;
                    case 7:
                        respList.addAll(getFiSource(req, TXN_MODE_TRUE_WALLET));
                        break;
                    case 101:
                        respList.addAll(getFiSource(req, TXN_MODE_VNPAY_ZALO_QR));
                        break;
                    case 102:
                        respList.addAll(getFiSource(req, TXN_MODE_VNPAY_ZALO_QR));
                        break;
                    case 103:
                        respList.addAll(getFiSource(req, TXN_MODE_VNPAY_ZALO_QR));
                        break;
                    default:
                        continue;
                }
            }
        }
        if (respList.size() > 0) {
            // 排序
            respList = respList.stream()
                    .sorted(Comparator.comparing(MerchantStatementResp::getTransactionDate).thenComparing(MerchantStatementResp::getTransactionId).reversed())
                    .collect(Collectors.toList());

            // 查询结果处理
            respList = manualSort(respList);
            // 余额校准
            if (type == null || type.size() == 0) {
                respList = getRecalculateBalance(respList);
            }
        }
        return respList;
    }

    /**
     * 计算小计或总计数据
     * @param respList
     * @return
     */
    private Map<String, BigDecimal> getSubTotalsOrTotals(List<MerchantStatementResp> respList) {

        BigDecimal credit = new BigDecimal(0);
        BigDecimal debit = new BigDecimal(0);
        BigDecimal rate = new BigDecimal(0);
        BigDecimal bankFee = new BigDecimal(0);

        for (MerchantStatementResp resp : respList) {
            credit = credit.add(ObjectUtils.isEmpty(resp.getCredit()) ? BigDecimal.ZERO : resp.getCredit());
            debit = debit.add(ObjectUtils.isEmpty(resp.getDebit()) ? BigDecimal.ZERO : resp.getCredit());
            rate = rate.add(ObjectUtils.isEmpty(resp.getRate()) ? BigDecimal.ZERO : resp.getCredit());
            bankFee = bankFee.add(ObjectUtils.isEmpty(resp.getBankFee()) ? BigDecimal.ZERO : resp.getCredit());
        }
        Map<String, BigDecimal> subTotal = new HashMap<>();
        subTotal.put("credit", credit);
        subTotal.put("debit", debit);
        subTotal.put("rate", rate);
        subTotal.put("bankFee", bankFee);

        return subTotal;
    }

    /**
     * 重新计算余额，校准余额
     *
     * @param respList
     * @return
     */
    private List<MerchantStatementResp> getRecalculateBalance(List<MerchantStatementResp> respList) {
        BigDecimal earlyBalance = new BigDecimal(0);
        for (int i = 0; i < respList.size(); i++) {
            if (i != 0){
                respList.get(i).setBalance(earlyBalance);
            }
            BigDecimal balance = respList.get(i).getBalance();
            BigDecimal requestAmount = respList.get(i).getRequestAmount();
            BigDecimal debit = ObjectUtils.isEmpty(respList.get(i).getDebit()) ? BigDecimal.ZERO : respList.get(i).getDebit();
            BigDecimal credit = ObjectUtils.isEmpty(respList.get(i).getCredit()) ? BigDecimal.ZERO : respList.get(i).getCredit();
            BigDecimal rate = ObjectUtils.isEmpty(respList.get(i).getRate()) ? BigDecimal.ZERO : respList.get(i).getRate();
            BigDecimal bankFee = (!ObjectUtils.isEmpty(respList.get(i).getPayBankFee()) && respList.get(i).getPayBankFee() == 1) ? respList.get(i).getBankFee() : BigDecimal.ZERO;

            if (requestAmount.compareTo(debit.add(credit)) != 0
                    && respList.get(i).getAltId().startsWith("W")) {
                debit = debit.compareTo(BigDecimal.ZERO) != 0 ? requestAmount : BigDecimal.ZERO;
                credit = credit.compareTo(BigDecimal.ZERO) != 0 ? requestAmount : BigDecimal.ZERO;
            }

            /** 计算前一笔交易的余额
             * 前一笔金额 = 余额 + 转出 - 转入 + 交易费 + (银行费用承担 == 商户 ? 银行手续费 : 0)
             * early_balance = balance + debit - credit + rate + (pay_bank_fee == 1 ? bank_fee : 0)
             */
            earlyBalance = balance.add(debit).subtract(credit).add(rate).add(bankFee).setScale(6, BigDecimal.ROUND_DOWN);
        }
        return respList;
    }

    /**
     * 手动排序，当两笔订单交易时间相同时，充值的金额大的放上边，代付下发的金额大的放下边
     * @return
     */
    private List<MerchantStatementResp> manualSort(List<MerchantStatementResp> respList){
        for (int i = 1; i < respList.size(); i++) {
            if ( // 充值
                    (respList.get(i).getTransactionDate().equals(respList.get(i - 1).getTransactionDate())
                    && respList.get(i).getType() == 0
                    && respList.get(i - 1).getType() == 0
                    && respList.get(i).getBalance().compareTo(respList.get(i - 1).getBalance()) > 0)
                ||  // 代付、下发
                    (respList.get(i).getTransactionDate().equals(respList.get(i - 1))
                    && respList.get(i).getType() == 1 || respList.get(i).getType() == 2
                    && respList.get(i - 1).getType() == 1 || respList.get(i - 1).getType() == 2
                    && respList.get(i).getBalance().compareTo(respList.get(i - 1).getBalance()) < 0)
            ) {
                MerchantStatementResp temp = respList.get(i);
                Collections.swap(respList, i, i - 1);
                respList.set(i - 1, temp);
            }
        }
        return respList;
    }

    /**
     * 查询充值数据
     * @param req
     * @return
     */
    private List<MerchantStatementResp> getFiSource(MerchantStatementReq req, Integer txnMode) {
        JoinLambdaWrapper<SysFiOrderEntity> fiWrapper = new JoinLambdaWrapper<>(SysFiOrderEntity.class);

        fiWrapper.eq(StringUtils.hasText(req.getCurrency()), SysFiOrderEntity::getCurrency, req.getCurrency())
                .eq(!ObjectUtils.isEmpty(req.getMerchantId()), SysFiOrderEntity::getMerchantId, req.getMerchantId())
                .between(SysFiOrderEntity::getUpdatedAt, req.getTransactionStartDate(), req.getTransactionEndDate())
                .eq(StringUtils.hasText(req.getTransactionId()), SysFiOrderEntity::getId, req.getMerchantId())
                .eq(SysFiOrderEntity::getStatus, STATUS_COMPLETED)
                .eq(!ObjectUtils.isEmpty(txnMode), SysFiOrderEntity::getTxnMode, txnMode)
                .selectAs(fi -> {
                    fi.add(SysFiOrderEntity::getBankFee);
                    fi.add(SysFiOrderEntity::getRate);
                    fi.addFunAlias(SysFiOrderEntity::getMerchantWalletBalance, MerchantStatementResp::getBalance);
                    fi.addFunAlias(SysFiOrderEntity::getNote, MerchantStatementResp::getDescription);
                    fi.add("IFNULL(sys_fi_order.completed_at, sys_fi_order.updated_at)", "transactionDate", false);
                    fi.add("0", "type", false);
                    fi.add(SysFiOrderEntity::getTxnMode);
                    fi.add(SysFiOrderEntity::getRequestAmount);
                    fi.addFunAlias(SysFiOrderEntity::getPaidAmount, MerchantStatementResp::getCredit);
                    fi.addFunAlias(SysFiOrderEntity::getId, MerchantStatementResp::getTransactionId);
                    fi.add("CONCAT('D" + "-', DATE_FORMAT(sys_fi_order.created_at, '%d%m%y'), if (sys_fi_order.id >= 1000, sys_fi_order.id, lpad(sys_fi_order.id, 4, '0')))", "altId", false);
                });
        fiWrapper.leftJoin(SysMerchantInfo.class, SysMerchantInfo::getId, SysFiOrderEntity::getMerchantId)
                .selectAs(mer -> {
                    mer.addFunAlias(SysMerchantInfo::getName, MerchantStatementResp::getMerchantName);
                    mer.addFunAlias(SysMerchantInfo::getCode, MerchantStatementResp::getMerchantCode);
                })
                .end();
        List<MerchantStatementResp> list = fiService.joinList(fiWrapper, MerchantStatementResp.class);
        return list;
    }

    /**
     * 查询下发数据
     * @param req
     * @return
     */
    private List<MerchantStatementResp> getFxSource(MerchantStatementReq req) {
        JoinLambdaWrapper<SysFxOrderEntity> fxWrapper = new JoinLambdaWrapper<>(SysFxOrderEntity.class);

        fxWrapper.eq(StringUtils.hasText(req.getCurrency()), SysFxOrderEntity::getCurrency, req.getCurrency())
                .eq(!ObjectUtils.isEmpty(req.getMerchantId()), SysFxOrderEntity::getMerchantId, req.getMerchantId())
                .between(SysFxOrderEntity::getUpdatedAt, req.getTransactionStartDate(), req.getTransactionEndDate())
                .eq(StringUtils.hasText(req.getTransactionId()), SysFxOrderEntity::getId, req.getMerchantId())
                .eq(SysFxOrderEntity::getStatus, STATUS_COMPLETED)
                .selectAs(fx -> {
                    fx.add(SysFxOrderEntity::getBankFee);
                    fx.add(SysFxOrderEntity::getRate);
                    fx.addFunAlias(SysFxOrderEntity::getMerchantWalletBalance, MerchantStatementResp::getBalance);
                    fx.addFunAlias(SysFxOrderEntity::getNote, MerchantStatementResp::getDescription);
                    fx.addFunAlias(SysFxOrderEntity::getUpdatedAt, MerchantStatementResp::getTransactionDate);
                    fx.add("2", "type", false);
                    fx.add(SysFxOrderEntity::getRequestAmount);
                    fx.addFunAlias(SysFxOrderEntity::getPaidAmount, MerchantStatementResp::getDebit);
                    fx.addFunAlias(SysFxOrderEntity::getId, MerchantStatementResp::getTransactionId);
                    fx.add("CONCAT('S" + "-', DATE_FORMAT(sys_fx_order.created_at, '%d%m%y'), if (sys_fx_order.id >= 1000, sys_fx_order.id, lpad(sys_fx_order.id, 4, '0')))", "altId", false);
                });
        fxWrapper.leftJoin(SysMerchantInfo.class, SysMerchantInfo::getId, SysFxOrderEntity::getMerchantId)
                .selectAs(mer -> {
                    mer.addFunAlias(SysMerchantInfo::getName, MerchantStatementResp::getMerchantName);
                    mer.addFunAlias(SysMerchantInfo::getCode, MerchantStatementResp::getMerchantCode);
                })
                .end();
        List<MerchantStatementResp> list = fxOrderEntityService.joinList(fxWrapper, MerchantStatementResp.class);
        return list;
    }

    /**
     * 查询代付数据
     * @param req
     * @return
     */
    private List<MerchantStatementResp> getFoSource(MerchantStatementReq req) {
        JoinLambdaWrapper<FoOrderMainEntity> foWrapper = new JoinLambdaWrapper<>(FoOrderMainEntity.class);

        foWrapper.eq(StringUtils.hasText(req.getCurrency()), FoOrderMainEntity::getCurrency, req.getCurrency())
                .eq(!ObjectUtils.isEmpty(req.getMerchantId()), FoOrderMainEntity::getMerchantId, req.getMerchantId())
                .between(FoOrderMainEntity::getUpdatedAt, req.getTransactionStartDate(), req.getTransactionEndDate())
                .eq(StringUtils.hasText(req.getTransactionId()), FoOrderMainEntity::getId, req.getMerchantId())
                .eq(FoOrderMainEntity::getStatus, STATUS_COMPLETED)
                .selectAs(fo -> {
                    fo.add(FoOrderMainEntity::getPayBankFee);
                    fo.add(FoOrderMainEntity::getBankFee);
                    fo.add(FoOrderMainEntity::getRate);
                    fo.addFunAlias(FoOrderMainEntity::getMerchantWalletBalance, MerchantStatementResp::getBalance);
                    fo.addFunAlias(FoOrderMainEntity::getNote, MerchantStatementResp::getDescription);
                    fo.addFunAlias(FoOrderMainEntity::getUpdatedAt, MerchantStatementResp::getTransactionDate);
                    fo.add("1", "type", false);
                    fo.add(FoOrderMainEntity::getRequestAmount);
                    fo.addFunAlias(FoOrderMainEntity::getPaidAmount, MerchantStatementResp::getDebit);
                    fo.addFunAlias(FoOrderMainEntity::getId, MerchantStatementResp::getTransactionId);
                    fo.add("CONCAT('W" + "-', DATE_FORMAT(sys_fo_order_main.created_at, '%d%m%y'), if (sys_fo_order_main.id >= 1000, sys_fo_order_main.id, lpad(sys_fo_order_main.id, 4, '0')))", "altId", false);
                });
        foWrapper.leftJoin(SysMerchantInfo.class, SysMerchantInfo::getId, FoOrderMainEntity::getMerchantId)
                .selectAs(mer -> {
                    mer.addFunAlias(SysMerchantInfo::getName, MerchantStatementResp::getMerchantName);
                    mer.addFunAlias(SysMerchantInfo::getCode, MerchantStatementResp::getMerchantCode);
                })
                .end();
        List<MerchantStatementResp> list = foOrderMainService.joinList(foWrapper, MerchantStatementResp.class);
        return list;
    }

    /**
     * 查询sys_adjust_order数据
     * @param req
     * @return
     */
    private List<MerchantStatementResp> getAjSource(MerchantStatementReq req) {
        JoinLambdaWrapper<SysAdjustOrderEntity> adWrapper = new JoinLambdaWrapper<>(SysAdjustOrderEntity.class);

        StringBuilder typeCase = new StringBuilder();
        typeCase.append("WHEN " + TYPE_DEPOSIT + " THEN " + 0);
        typeCase.append(" WHEN " + TYPE_WITHDRAW + " THEN " + 1);
        typeCase.append(" WHEN " + TYPE_TOPUP + " THEN " + 3);
        typeCase.append(" WHEN " + TYPE_BALANCE + " THEN " + 4);
        typeCase.append(" WHEN " + TYPE_SETTLEMENT + " THEN " + 2);

        adWrapper.eq(StringUtils.hasText(req.getCurrency()), SysAdjustOrderEntity::getCurrency, req.getCurrency())
                .eq(!ObjectUtils.isEmpty(req.getMerchantId()), SysAdjustOrderEntity::getMerchantId, req.getMerchantId())
                .between(SysAdjustOrderEntity::getUpdatedAt, req.getTransactionStartDate(), req.getTransactionEndDate())
                .eq(StringUtils.hasText(req.getTransactionId()), SysAdjustOrderEntity::getId, req.getMerchantId())
                .in(!ObjectUtils.isEmpty(req.getType()), SysAdjustOrderEntity::getType, req.getType())
                .eq(SysAdjustOrderEntity::getStatus, STATUS_APPROVED)
                .selectAs(aj -> {
                    aj.addFunAlias(SysAdjustOrderEntity::getMerchantWalletBalance, MerchantStatementResp::getBalance);
                    aj.add(SysAdjustOrderEntity::getDescription);
                    aj.add(SysAdjustOrderEntity::getReason);
                    aj.addFunAlias(SysAdjustOrderEntity::getAmount, MerchantStatementResp::getRequestAmount);
                    aj.addFunAlias(SysAdjustOrderEntity::getUpdatedAt, MerchantStatementResp::getTransactionDate);
                    aj.addFunAlias(SysAdjustOrderEntity::getId, MerchantStatementResp::getTransactionId);
                    aj.add("CASE sys_adjust_order.type " + typeCase.toString() + " END", "type", false);
                    aj.add("CASE sys_adjust_order.txn_type WHEN " + TXN_TYPE_CREDIT + " THEN sys_adjust_order.amount ELSE 0 END", "credit", false);
                    aj.add("CASE sys_adjust_order.txn_type WHEN " + TXN_TYPE_DEBIT + " THEN sys_adjust_order.amount ELSE 0 END", "debit", false);
                    aj.add("CONCAT('A" + "-', DATE_FORMAT(sys_adjust_order.created_at, '%d%m%y'), if (sys_adjust_order.id >= 1000, sys_adjust_order.id, lpad(sys_adjust_order.id, 4, '0')))", "altId", false);
                });
        adWrapper.leftJoin(SysMerchantInfo.class, SysMerchantInfo::getId, SysAdjustOrderEntity::getMerchantId)
                .selectAs(mer -> {
                    mer.addFunAlias(SysMerchantInfo::getName, MerchantStatementResp::getMerchantName);
                    mer.addFunAlias(SysMerchantInfo::getCode, MerchantStatementResp::getMerchantCode);
                })
                .end();
        List<MerchantStatementResp> list = adjustOrderService.joinList(adWrapper, MerchantStatementResp.class);
        return list;
    }
}
